package org.zw.android.framework.async;

/**
 * Asynchronous Task Runnable
 * 
 * @author zhouwei
 *
 */
public abstract class AsyncTaskRunnable {
	
	private AsyncTaskHandler mHandler ;
	
	/**
	 * Default Constructor, the TaskHandler is null
	 */
	public AsyncTaskRunnable(){
		this(null);
	}
	
	public AsyncTaskRunnable(AsyncTaskHandler handler){
		mHandler	= handler ;
	}
	
	public final AsyncTaskHandler getHandler(){
		return mHandler ;
	}
	
	/**
	 * Notify UI Exception
	 * 
	 * @param e
	 */
	public final void notifyException(Exception e){
		
		if(mHandler != null){
			mHandler.obtainMessage(AsyncTaskHandler.MSG_STATUS_ERROR, e).sendToTarget();
		}
	}

	/**
	 * on Start
	 * 
	 * @return
	 */
	public boolean onStart() {
		return true;
	}
	
	/**
	 * on Processing
	 * 
	 * @return if return is null, the task is failed
	 */
	public abstract Object onProcessing();

	/**
	 * on Finish
	 * 
	 * @param value
	 */
	public void onFinish(Object value){
		
	}

	/**
	 * on Exception
	 * @param e
	 */
	public void onException(Throwable e) {
		
	}
	
	/**
	 * on Cancel
	 */
	public void onCancel() {
		
	}
}
